#include <deque>
#include <mutex>
#include <condition_variable>
#include <chrono>
#include <stdexcept>

template<class T>
class BlockQueue {
public:
    explicit BlockQueue(size_t maxSize = 1000) 
        : m_maxSize(maxSize) 
    {
        if(maxSize <= 0) {
            throw std::invalid_argument("Queue size must be positive");
        }
    }

    bool push(const T& item) {
        std::unique_lock<std::mutex> lock(m_mutex);
        
        // 等待队列未满
        m_cond_not_full.wait(lock, [this] {
            return m_queue.size() < m_maxSize;
        });


        m_queue.push_back(item);
        m_cond_not_empty.notify_one();
        return true;
    }

    bool pop(T& item, int timeout_ms = -1) {
        std::unique_lock<std::mutex> lock(m_mutex);
        
        auto pred = [this] { return !m_queue.empty(); };
        
        if(timeout_ms < 0) {
            m_cond_not_empty.wait(lock, pred);
        } else {
            auto timeout = std::chrono::steady_clock::now() 
                         + std::chrono::milliseconds(timeout_ms);
            if(!m_cond_not_empty.wait_until(lock, timeout, pred)) {
                return false;
            }
        }

        if(m_queue.empty()) return false;

        item = std::move(m_queue.front());
        m_queue.pop_front();
        m_cond_not_full.notify_one();
        return true;
    }

    // 其他辅助方法...

private:
    mutable std::mutex m_mutex;
    std::condition_variable m_cond_not_empty;
    std::condition_variable m_cond_not_full;
    std::deque<T> m_queue;
    size_t m_maxSize;
};